package com.android.hedgehog;

import java.util.List;

import android.util.Log;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;

public class FreePlayMenu extends Screen {
	enum page {
		page1, page2, page3
	}
	page currentPage = page.page1;

	public FreePlayMenu(Game game) {
		super(game);
	}

	@Override
	public void update(float deltaTime) {
		Graphics g = game.getGraphics();
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

		int len = touchEvents.size();

		switch (currentPage) {
			case page1 :
				updatePage1(deltaTime, g, touchEvents, len);
				break;
			case page2 :
				updatePage2(deltaTime, g, touchEvents, len);
				break;
			case page3 :
				updatePage3(deltaTime, g, touchEvents, len);
				break;
		}

	}

	/**
	 * Update() method that holds the first 5 games
	 */
	private void updatePage1(float deltaTime, Graphics g,
			List<TouchEvent> touchEvents, int len) {

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.y < 80) {
					game.setScreen(new FreePlayScreen2(game, 0,0));
					soundEffect();
					return;
				}
				if (event.y > 80 && event.y < 160) {
					game.setScreen(new FreePlayScreen2(game, 1,1));
					soundEffect();
					return;
				}
				if (event.y > 160 && event.y < 240) {
					game.setScreen(new FreePlayScreen2(game, 2,2));
					soundEffect();
					return;
				}
				if (event.y > 240 && event.y < 320) {
					game.setScreen(new FreePlayScreen2(game, 3,1));
					soundEffect();
					return;
				}
				if (event.y > 320 && event.y < 400) {
					//game.setScreen(new FreePlayScreen(game, 4));
					soundEffect();
					return;
				}
				if (event.y > 420 && (event.x < 60 || event.x > 260)) {
					newPage(event);
					return;
				}
			}
		}

	}

	/**
	 * Update() method that holds games 6-10
	 */
	private void updatePage2(float deltaTime, Graphics g,
			List<TouchEvent> touchEvents, int len) {

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.y < 80) {
					//game.setScreen(new FreePlayScreen(game, 5));
					soundEffect();
					return;
				}
				if (event.y > 80 && event.y < 160) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 160 && event.y < 240) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 240 && event.y < 320) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 320 && event.y < 400) {
					//game.setScreen(new RisingConnect(game));
					soundEffect();
					return;
				}
				if (event.y > 420 && (event.x < 60 || event.x > 260)) {
					newPage(event);
					return;
				}
			}
		}

	}

	/**
	 * Update() method that holds games 11-16
	 */
	private void updatePage3(float deltaTime, Graphics g,
			List<TouchEvent> touchEvents, int len) {

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.y < 80) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 80 && event.y < 160) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 160 && event.y < 240) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 240 && event.y < 320) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 320 && event.y < 400) {
					// game.setScreen(new YourGameHere(game));
					soundEffect();
					return;
				}
				if (event.y > 420 && (event.x < 60 || event.x > 260)) {
					newPage(event);
					return;
				}
			}
		}

	}

	private synchronized void newPage(TouchEvent event) {
		if (currentPage == page.page1) { // if (page 1)
			if (event.x < 60) {
				game.setScreen(new MainMenu(game));
				dispose();
				soundEffect();
			} else {
				currentPage = page.page2;
				soundEffect();
			}
		} else if (currentPage == page.page2) { // if (page 2)
			if (event.x < 60) {
				currentPage = page.page1;
				soundEffect();
			} else {
				currentPage = page.page3;
				soundEffect();
			}
		} else { // page 3
			if (event.x < 60) {
				currentPage = page.page2;
				soundEffect();
			}
		}

	}

	private void soundEffect() {
		if (Settings.soundEnabled)
			Assets.bitten.play(1);
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.drawPixmap(Assets.whitebackground, 0, 0);

		if (currentPage == page.page1) {
			g.drawPixmap(Assets.crazyboxesbanner, 0, 0);
			g.drawPixmap(Assets.triplesumbanner, 0, 80);
			g.drawPixmap(Assets.datamatrixbanner, 0, 160);
			g.drawPixmap(Assets.emptybanner, 0, 240);
			g.drawPixmap(Assets.emptybanner, 0, 320);
			g.drawPixmap(Assets.blackarrowbuttons, 0, 420, 120, 0, 60, 60);
			g.drawPixmap(Assets.blackarrowbuttons, 260, 420, 0, 0, 60, 60);
		} else if (currentPage == page.page2) {
			g.drawPixmap(Assets.emptybanner, 0, 0);
			g.drawPixmap(Assets.emptybanner, 0, 80);
			g.drawPixmap(Assets.emptybanner, 0, 160);
			g.drawPixmap(Assets.emptybanner, 0, 240);
			g.drawPixmap(Assets.emptybanner, 0, 320);
			g.drawPixmap(Assets.blackarrowbuttons, 0, 420, 60, 0, 60, 60);
			g.drawPixmap(Assets.blackarrowbuttons, 260, 420, 0, 0, 60, 60);
		} else {
			g.drawPixmap(Assets.emptybanner, 0, 0);
			g.drawPixmap(Assets.emptybanner, 0, 80);
			g.drawPixmap(Assets.emptybanner, 0, 160);
			g.drawPixmap(Assets.emptybanner, 0, 240);
			g.drawPixmap(Assets.emptybanner, 0, 320);
			g.drawPixmap(Assets.blackarrowbuttons, 0, 420, 60, 0, 60, 60);
		}
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
